import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/xindong_common_get_code_view.dart';

class XinDongDestroyYeMian extends StatefulWidget {
  const XinDongDestroyYeMian({super.key});

  @override
  State<XinDongDestroyYeMian> createState() => _XinDongDestroyYeMianState();
}

class _XinDongDestroyYeMianState extends State<XinDongDestroyYeMian> {
  String _code = "";

  void _onSubmit() async {
    if (_code.length < 4) {
      showToast("请输入验证码");
      return;
    }
    showLoading();
    var parameters = {"code": _code};
    final object = await XinDongNetworking.instance.request(
      XinDongApi.deleteAccount,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    hideLoading();
    if (object.code == XinDongServerStatus.success) {
      XinDongUserController.to.passiveLogout(showTips: "账号已注销，请切换账号登录。");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("注销账号"),
      body: GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: ListView(
          children: [
            XinDongCommonGetCodeView(
              onChange: (String code) => _code = code,
            ),
            const SizedBox(height: 56),
            Container(
              height: kButtonHeight,
              margin: const EdgeInsets.only(left: 20, right: 20),
              child: XinDongCommonButton("立即注销", onTap: _onSubmit),
            ),
          ],
        ),
      ),
    );
  }
}
